package me.zk.practice1.s;

/**
 * @Author: zhang kai
 * @Date: 2023/05/10
 * @Description:
 */
public class S18 {
    public static void main(String[] args) {
       System.out.println( maxAreaOfIsland(new int[][]{{1,0,1,1,0,1,0,1},{1,0,1,1,0,1,1,1},{0,0,0,0,0,0,0,1}}));
    }

    public static int maxAreaOfIsland(int[][] grid) {
        int maxArea = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                maxArea = Math.max(maxArea,dfs(grid,i,j));
            }
        }

        return maxArea;
    }

    public static int dfs(int[][] grid, int cur_i, int cur_j) {
        System.out.println(cur_i+"," + cur_i);
        if (cur_i < 0 || cur_j < 0 || cur_i == grid.length || cur_j == grid[0].length || grid[cur_i][cur_j] != 1) {
            return 0;
        }
        grid[cur_i][cur_j] = 0;
        //上下左右
        int[] di = {0, 0, 1, -1};
        int[] dj = {1, -1, 0, 0};
        int ans = 1;
        for (int index = 0; index != 4; ++index) {
            int next_i = cur_i + di[index], next_j = cur_j + dj[index];
            ans += dfs(grid, next_i, next_j);
        }
        return ans;
    }

}
